package com.jbm.jbmsupplier.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;


/**
 * Created by 家帮忙 on 2017/1/11.
 */

public class DialogLoading extends Dialog {
    Context context;
    private String textstr = "loading......";//需要加载的内容
    private Boolean dialogshow;//检测按返回键false值是销毁进度框，true是不销毁
    private Animation anim;
    private ImageView iv;
    private TextView tv;

    public DialogLoading(@NonNull Context context) {
        super(context, R.style.dialogLoading);
        this.context = context;
    }

    public DialogLoading(@NonNull Context context, String textstr) {
        super(context, R.style.dialogLoading);
        this.context = context;
        this.textstr = textstr;

    }

    public DialogLoading(@NonNull Context context, Boolean dialogshow) {
        super(context, R.style.dialogLoading);
        this.context = context;
        this.dialogshow = dialogshow;

    }

    public DialogLoading(@NonNull Context context, String textstr, Boolean dialogshow) {
        super(context, R.style.dialogLoading);
        this.context = context;
        this.textstr = textstr;
        this.dialogshow = dialogshow;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        requestWindowFeature(Window.PROGRESS_VISIBILITY_ON);//进度条的可见度
        anim = AnimationUtils.loadAnimation(context, R.anim.rptate_repate);
        anim.setInterpolator(new LinearInterpolator());
        iv = new ImageView(context);
        iv.setImageResource(R.mipmap.spinner_black);
        iv.setAnimation(anim);
         tv = new TextView(context);
        tv.setText(textstr);
        tv.setTextColor(context.getResources().getColor(R.color.textdialogcolor));
        tv.setPadding(0, 20, 0, 0);
        //创建布局和设置布局沾满全屏
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.CENTER;
        LinearLayout linearlayout = new LinearLayout(context);
        linearlayout.setOrientation(LinearLayout.VERTICAL);
        linearlayout.setBackgroundResource(R.drawable.shapdilog);
        lp.leftMargin=20;
        lp.rightMargin=20;
        setContentView(linearlayout, lp);
        linearlayout.addView(iv, lp);
        linearlayout.addView(tv, lp);
        setCanceledOnTouchOutside(false);//取消点击事件
    }

    public void setTextstr(String name){
        tv.setText(name);    }


//点击返回的时候如果传过来的是true就要关闭进度
    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (dialogshow) {
                this.dismiss();
            }
            return true;
        }
        return false;


    }

    @Override
    public void show() {
        if (this != null) {
            if (!super.isShowing()) {
                super.show();
                iv.startAnimation(anim);
            }
        }
    }

    @Override
    public void dismiss() {
        if (this != null) {
            if (super.isShowing()) {
                super.dismiss();
            }
        }
    }
}
